package com.xbongbong.paas.help.symbol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;

import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/01/07 15:22
 */

public class AddTimeSymbolHandle extends DateTimeSymbolHandle {


    @Override
    public boolean equals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getAddTime();
        if (Objects.isNull(dateTime)) {
            dateTime = DateTimeUtil.getInt();
        }
        dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        target = Long.valueOf(target.toString());
        return equals(target, dateTime);
    }

    @Override
    public boolean noEquals(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getAddTime();
        if (Objects.isNull(dateTime)) {
            dateTime = DateTimeUtil.getInt();
        }
        dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        target = Long.valueOf(target.toString());
        return noEquals(target, dateTime);
    }

    @Override
    public boolean greaterEqual(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getAddTime();
        if (Objects.isNull(dateTime)) {
            dateTime = DateTimeUtil.getInt();
        }
        dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        Number b = (Number) target;
        return greaterEqual(dateTime, b);
    }

    @Override
    public boolean lessEqual(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getAddTime();
        if (Objects.isNull(dateTime)) {
            dateTime = DateTimeUtil.getInt();
        }
        dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        Number b = (Number) target;
        return lessEqual(dateTime, b);
    }

    @Override
    public boolean greaterThan(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getAddTime();
        if (Objects.isNull(dateTime)) {
            dateTime = DateTimeUtil.getInt();
        }
        dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        Number b = (Number) target;
        return greaterThan(dateTime, b);
    }

    @Override
    public boolean lessThan(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getAddTime();
        if (Objects.isNull(dateTime)) {
            dateTime = DateTimeUtil.getInt();
        }
        dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        Number b = (Number) target;
        return lessThan(dateTime, b);
    }

    @Override
    public boolean range(Object target, PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        Long dateTime = source.getAddTime();
        if (Objects.isNull(dateTime)) {
            dateTime = DateTimeUtil.getInt();
        }
        dateTime = PaasDateUtil.getBeforeDayBreak(dateTime);
        JSONArray jsonObject = JSONObject.parseArray(JSON.toJSONString(target));
        Number min = (Number) jsonObject.get(0);
        Number max = (Number) jsonObject.get(1);
        return range(min, max, dateTime);
    }


    @Override
    public boolean empty(PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return empty(source.getAddTime());
    }

    @Override
    public boolean noEmpty(PaasFormDataEntity source, FieldAttrEntity fieldAttrEntity) throws XbbException {
        return noEmpty(source.getAddTime());
    }
}
